﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Text;

namespace WeSSysUser.util
{
    public class Common
    {
        public static string strDateTimeToString(string strDateTime)
        {
            strDateTime = strDateTime.Replace("/", "");
            strDateTime = strDateTime.Replace("-", "");
            strDateTime = strDateTime.Replace(" ", "");
            strDateTime = strDateTime.Replace(":", "");
            return strDateTime;
        }

        public static string getRandomString(int size)
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString().ToLower();
        }
        public static string generateHash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            Byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (Byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string outstr = s.ToString();
            return outstr;
        }

        public static DateTime strToFormatDateTime(string strDateTime, string dtFormat)
        {
            CultureInfo provider = CultureInfo.InvariantCulture;
            return DateTime.ParseExact(strDateTime, dtFormat, provider);
        }

        public static DateTime parseDate(string s, string format)
        {
            DateTime result;
            if (!DateTime.TryParse(s, out result))
            {
                result = DateTime.ParseExact(s, format, System.Globalization.CultureInfo.InvariantCulture);
            }
            return result;
        }

        public static DateTime strToDateTime(string strDateTime)
        {
            CultureInfo provider = CultureInfo.InvariantCulture;
            return DateTime.ParseExact(strDateTime.Trim(), "yyyyMMddHHmmss", provider);
        }

        public static string getComicRate(string rate)
        {
            double dRate = double.Parse(rate);
            if (dRate <= 1)
                return "0.png";
            if (dRate < 2)
                return "1.png";
            if (dRate < 3)
                return "2.png";
            if (dRate < 4)
                return "3.png";
            if (dRate < 5)
                return "4.png";
            if (dRate >= 5)
                return "5.png";
            return "0.png";
        }


        public static string getRate(string rate)
        {
            double dRate = double.Parse(rate);
            if (dRate < 0.5)
                return "iStar";
            if (dRate < 1)
                return "iStar i_s0-5";
            if (dRate < 1.5)
                return "iStar i_s1";
            if (dRate < 2)
                return "iStar i_s1-5";
            if (dRate < 2.5)
                return "iStar i_s2";
            if (dRate < 3)
                return "iStar i_s2-5";
            if (dRate < 3.5)
                return "iStar i_s3";
            if (dRate < 4)
                return "iStar i_s3-5";
            if (dRate < 4.5)
                return "iStar i_s4";
            if (dRate < 5)
                return "iStar i_s4-5";
            if (dRate >= 5)
                return "iStar i_s5";
            return "iStar";
        }

        public static string getLikeRate(string likeRate)
        {
            double dLikeRate = double.Parse(likeRate);
            if (dLikeRate < 0.5)
                return "iHeart";
            if (dLikeRate < 1)
                return "iHeart i_h0-5";
            if (dLikeRate < 1.5)
                return "iHeart i_h1";
            if (dLikeRate < 2)
                return "iHeart i_h1-5";
            if (dLikeRate < 2.5)
                return "iHeart i_h2";
            if (dLikeRate < 3)
                return "iHeart i_h2-5";
            if (dLikeRate < 3.5)
                return "iHeart i_h3";
            if (dLikeRate < 4)
                return "iHeart i_h3-5";
            if (dLikeRate < 4.5)
                return "iHeart i_h4";
            if (dLikeRate < 5)
                return "iHeart i_h4-5";
            if (dLikeRate >= 5)
                return "iHeart i_h5";
            return "iHeart";
        }

    }
}